# The list of individual toadlet base libraries
set (TOADLET_LIBRARY_LIST
	egg
	flick
	hop
	peeper
	ribbit
	tadpole
	pad
)


# Add the individual toadlet base libraries
foreach (TOADLET_LIBRARY ${TOADLET_LIBRARY_LIST})
	add_subdirectory (${TOADLET_LIBRARY})
endforeach (TOADLET_LIBRARY)


# Create the aggregate toadlet include files
# This uses the libraryname_HDR variable from each base library 
# As well as the libraryname_NAMESPACES variable for the namespaces
foreach (TOADLET_LIBRARY ${TOADLET_LIBRARY_LIST})
	string (TOUPPER ${TOADLET_LIBRARY} UPPERNAME)
	set (HEADER_FILE "${toadlet_BINARY_DIR}/source/cpp/toadlet/${TOADLET_LIBRARY}.h")

	file (WRITE ${HEADER_FILE} "/* This file is autogenerated */\n")
	file (APPEND ${HEADER_FILE} "#ifndef TOADLET_${UPPERNAME}_H\n")
	file (APPEND ${HEADER_FILE} "#define TOADLET_${UPPERNAME}_H\n")
	file (APPEND ${HEADER_FILE} "\n")

	foreach (H_FILE ${${UPPERNAME}_HDR})
		file (APPEND ${HEADER_FILE} "#include <toadlet/${TOADLET_LIBRARY}/${H_FILE}>\n")
	endforeach (H_FILE)

	file (APPEND ${HEADER_FILE} "\n")
	
	file (APPEND ${HEADER_FILE} "namespace toadlet{\n")
	file (APPEND ${HEADER_FILE} "using namespace toadlet::${TOADLET_LIBRARY};\n")
	foreach (NAMESPACE ${${UPPERNAME}_NAMESPACES})
		file (APPEND ${HEADER_FILE} "using namespace toadlet::${TOADLET_LIBRARY}::${NAMESPACE};\n")
	endforeach (NAMESPACE)
	file (APPEND ${HEADER_FILE} "}\n")

	file (APPEND ${HEADER_FILE} "\n")
	file (APPEND ${HEADER_FILE} "#endif\n")
endforeach (TOADLET_LIBRARY)


# Create the master toadlet include file
set (HEADER_FILE "${toadlet_BINARY_DIR}/source/cpp/toadlet/toadlet.h")

file (WRITE ${HEADER_FILE} "/* This file is autogenerated */\n")
file (APPEND ${HEADER_FILE} "#ifndef TOADLET_H\n")
file (APPEND ${HEADER_FILE} "#define TOADLET_H\n")
file (APPEND ${HEADER_FILE} "\n")

foreach (TOADLET_LIBRARY ${TOADLET_LIBRARY_LIST})
	file (APPEND ${HEADER_FILE} "#include <toadlet/${TOADLET_LIBRARY}.h>\n")
endforeach (TOADLET_LIBRARY)

file (APPEND ${HEADER_FILE} "\n")
file (APPEND ${HEADER_FILE} "#endif\n")


# Setup toadlet's configure file
configure_file (${CMAKE_CURRENT_SOURCE_DIR}/Config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/Config.h)


# Javascript library
if (TOADLET_PLATFORM_EMSCRIPTEN)
	add_executable (toadlet
		"../../embind/toadlet/egg/Bindings.cpp"
		"../../embind/toadlet/hop/Bindings.cpp"
		"../../embind/toadlet/peeper/Bindings.cpp"
		"../../embind/toadlet/tadpole/Bindings.cpp"
	)
	
	set_target_properties (toadlet PROPERTIES COMPILE_FLAGS "--bind")
	set_target_properties (toadlet PROPERTIES LINK_FLAGS "--bind --js-library ../../../../source/js/DOMArchive.js")
	target_link_libraries (toadlet "toadlet_egg_s" "toadlet_hop_s" "toadlet_peeper_s" "toadlet_peeper_gles2renderdevice_s" "toadlet_ribbit_s" "toadlet_ribbit_alaudiodevice_s" "toadlet_tadpole_s")
	get_target_property (TOADLET_JS_OUTPUT toadlet LOCATION)
	add_custom_command(TARGET toadlet POST_BUILD
		COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/Modules/JSModulify.py toadlet ${TOADLET_JS_OUTPUT} ${CMAKE_INSTALL_PREFIX}/${LIB_INSTALL_DIR}/toadlet.js)
endif (TOADLET_PLATFORM_EMSCRIPTEN)

# Install header files 
if (NOT INSTALL_ANDROID_APP)
	install (FILES ${toadlet_SOURCE_DIR}/source/cpp/toadlet/Types.h DESTINATION include/toadlet COMPONENT required)
	install (FILES ${toadlet_BINARY_DIR}/source/cpp/toadlet/Config.h DESTINATION include/toadlet COMPONENT required)
	foreach (TOADLET_LIBRARY ${TOADLET_LIBRARY_LIST})
		install (DIRECTORY ${toadlet_SOURCE_DIR}/source/cpp/toadlet/${TOADLET_LIBRARY} COMPONENT ${TOADLET_LIBRARY} DESTINATION include/toadlet FILES_MATCHING PATTERN "*" PATTERN "*.cpp" EXCLUDE PATTERN "*.txt" EXCLUDE PATTERN "*.cmake" EXCLUDE)
		install (FILES ${toadlet_BINARY_DIR}/source/cpp/toadlet/${TOADLET_LIBRARY}.h DESTINATION include/toadlet COMPONENT ${TOADLET_LIBRARY})

		# Each library's version file is configured by that library and installed here
		install (FILES  ${toadlet_BINARY_DIR}/source/cpp/toadlet/${TOADLET_LIBRARY}/Version.h DESTINATION include/toadlet/${TOADLET_LIBRARY} COMPONENT ${TOADLET_LIBRARY})
	endforeach (TOADLET_LIBRARY)
	install (FILES ${toadlet_BINARY_DIR}/source/cpp/toadlet/toadlet.h DESTINATION include/toadlet)
endif (NOT INSTALL_ANDROID_APP)

